package gameStates.gui;

import java.util.ArrayList;
import java.util.List;

import gameStates.PassGameState;
import gameStates.passes.AbsPassManagerState;
import input.FengJMEInputHandler;
import interfaces.AbsInterfaceElement;
import interfaces.superWidgets.InterfaceWindow;
import interfaces.superWidgets.MoneyListener;
import interfaces.superWidgets.UpdateWidget;

import main.listeners.listenerClasses.SettingsListener;

import org.fenggui.Display;
import org.fenggui.IWidget;
import org.fenggui.binding.render.dummy.DummyBinding;
import org.fenggui.binding.render.lwjgl.LWJGLBinding;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;

/**
 * This is the basic class for all {@link GameState}s that display elements of the graphical user 
 * interface.
 * 
 * @author Wasserleiche
 */
public abstract class GUIState extends PassGameState implements MoneyListener {
	
	/** The {@link Display} that shall be used to display FengGUI-Widgets. */
	protected Display guiDisp;
	/** The input-handler that is used for FengGUI. */
	protected FengJMEInputHandler guiInput;
	
	/** Contains all {@link IWidget}s that shall be displayed when the {@link GameState} is active. */
	/** Contains all {@link IWidget}s that shall be updated when the {@link GameState} is active. */
	protected List<UpdateWidget> updateWidgets;
	
	protected AbsInterfaceElement interfaceElement;
	
	/** The {@link TextureState} that is used as default. (don't ask me what this is for... forgot it) */
	protected TextureState defaultTextureState;
	
	public GUIState(String name, AbsPassManagerState passManagerState, boolean init) {
		this(name, passManagerState, init, false);
	}
	
	/**
	 * Constructs a new GUIState with the given information.
	 * @param name The {@link GameState}-Name.
	 * @param showMouse true, if this GUIState should show a mouse-cursor. false, if not.
	 */
	public GUIState(String name, AbsPassManagerState passManagerState, boolean init, boolean atEnd) {
		super(name, passManagerState, atEnd);
		
		if(init) {
			Texture defTex = TextureState.getDefaultTexture().createSimpleClone();
			defaultTextureState = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
			defTex.setScale(new Vector3f(1, 1, 1));
			defaultTextureState.setTexture(defTex);
		}
		
		updateWidgets = new ArrayList<UpdateWidget>();
		
		if(init) guiDisp = new Display(new LWJGLBinding());
		else guiDisp = new Display(new DummyBinding());
		
		guiInput = new FengJMEInputHandler(guiDisp);
		
		addListenerToClearer(new SettingsListener() {
			@Override
			public void graphicSettingsChanged() {
				restore();
			}
			
			@Override
			public void controlSettingsChanged() {}
			@Override
			public void commonSettingsChanged() {}
			@Override
			public void soundSettingsChanged() {}
		});
	}
	
	public void addWidget(IWidget widget) {
		if(widget instanceof InterfaceWindow) ((InterfaceWindow)widget).addToContainer(guiDisp);
		else guiDisp.addWidget(widget);
		
		if(widget instanceof UpdateWidget) updateWidgets.add((UpdateWidget)widget);
	}
	
	public void removeWidget(IWidget widget) {
		guiDisp.removeWidget(widget);
		updateWidgets.remove(widget);
	}
		
	@Override
	public void render(float tpf) {
		for(RenderState r : Renderer.defaultStateList) {
			r.apply();
		}
		defaultTextureState.apply();
		DisplaySystem.getDisplaySystem().getRenderer().draw(rootNode);
		guiDisp.display();
	}
	
	@Override
	public void update(float tpf) {
		if(!isActive()) return;
		
		if(guiInput.isEnabled()) {
			guiInput.update(tpf);
			rootNode.updateGeometricState(tpf, true);
			rootNode.updateRenderState();
		}
		
		for(UpdateWidget widget : updateWidgets) {
			if(widget != null) widget.update(tpf);
		}
	}
	
	/**
	 * Enables or disables this GUIState. The input, mouse and all windows will also be enabled/disabled.
	 */
	@Override
	public void setActive(boolean active) {
		super.setActive(active);
		enableInput(active);
		
		guiDisp.setVisible(active);
	}
	
	@Override
	public void removeGameState() {
		super.removeGameState();
		
		cleanup();
	}
	
	@Override
	public void cleanup() {
		super.cleanup();
		setActive(false);
		guiDisp.removeAllWidgets();
		updateWidgets.clear();
		rootNode.detachAllChildren();
		rootNode.clearControllers();
		guiInput.cleanup();
		
		interfaceElement = null;
		guiDisp = null;
		guiInput = null;
	}
	
	/**
	 * Restores this GUIState. The {@link Display} will be cleared as well as the windows. The size of the 
	 * {@link Display} will be updated to the size of {@link DisplaySystem} and the cursor will be 
	 * (re-)initialized.
	 */
	@Override
	public void restore() {
		super.restore();
		
		rootNode.detachAllChildren();
		guiDisp.removeAllWidgets();
		updateWidgets.clear();
		updateDisplaySize();
	}
	
	protected void updateDisplaySize() {
		guiDisp.setSize(DisplaySystem.getDisplaySystem().getWidth(), DisplaySystem.getDisplaySystem().getHeight());
	}
	
	@Override
	public void updateMoney(int newMoney) {
		if(interfaceElement != null) interfaceElement.updateMoney(newMoney);
	}
	
	@Override
	public void enableInput(boolean enable) {
		super.enableInput(enable);
		guiInput.setEnabled(enable);
	}
	
	public FengJMEInputHandler getInputHandler() { return guiInput; }
	
	public Display getGUIDisplay() { return guiDisp; }
	
	public int getWidth() { return guiDisp.getWidth(); }
	
	public int getHeight() { return guiDisp.getHeight(); }
}